<?php
/**
 * 数据字典管理
 * @author hushuai
 *
 */
class DictAction extends CommonAction{
	
	private $Dict  =  "Dict";  //定义数据字典数据表
	
	public function _initialize() {
		
		parent::_initialize();
		Vendor('Common.Tree');
	}
	
	/**
	 * 数据字典列表
	 */
	public function index(){
		
		$lang = getLang();
		$column  = sendRest("BDC/Dict/a_column");
		foreach($column['data'] as $k => $v) {
			$v['title'] = dejsonByLang($v['value'],$lang);  //json对应语言包值
			$array[]    = $v;
		}
		$this->assign("column",$array);
		$map['key'] = $this->_param("key","trim","");
		$this->assign("keys",$map['key']);
		if($map['key']){
			$json = sendRest("BDC/Dict/getChildColumn",$map);
			$list = $json['data'];
			$array = array();
			$add   = L('nod_add_child_title');
			$edit  = L('cfg_list_itf_btn_modify');
			$del   = L('cfg_list_itf_btn_delete');
				
			foreach($list as $k => $v) {
			
				$v['addUrl']  = U("Dict/add",    'id='.$v['id']);
				$v['updUrl']  = U("Dict/edit",   "id=".$v['id']."&pid=".$v["pid"]);
				if($v['haveLeaf'] == 1){
					$v['delUrl'] = '<a class="btn bg_gray white"  data-id="'.$v['id'].'"><i class="icon-remove"></i></a>';
				}else{
					$v['delUrl'] = '<a href="#" class="btn bg_red white delone" data-pid="'.$v['id'].'"  data-id="'.$v['id'].'"><i class="icon-remove"></i></a>';
				}
				$v['title']   = dejsonByLang($v['value'],$lang);  //json对应语言包值
				$array[]      = $v;
			}
				
			$str  = "<tr class='tr level_\$level' isleaf='\$isleaf' level='\$level' id='area_\$id' dataid='\$id'>
					     <td align='center'><input class='form_ele_text w100' type='text' value='\$sort' size='3' name='sort[\$id]'></td>
					     <td class='name'>\$spacer \$title</td>
						 <td align='center'>
							<a  href='\$addUrl' class='btn bg_green white' title ='".$add."'><i class='icon-plus'></i></a>
							<a  href='\$updUrl' class='btn bg_blue white'  title ='".$edit."'><i class='icon-edit'></i></a>
							\$delUrl
						 </td>
					 </tr>";
				
			$Tree = new Tree();
			$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
			$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
			$Tree->init($array);
			$htmlTree = $Tree->get_tree(0, $str);
			$this->assign("htmlTree",$htmlTree);
		}else{
			$this->assign("htmlTree",'');
		}
		$this->display();
	
	}
	/**
	 * 数据字典添加
	 */
	
	public function add(){
		
		$lang = getLang();
		$id = $this->_param("id","intval",0);
		
		if(IS_AJAX){
			$json = sendRest("BDC/Dict/add", $_POST);
			$this->ajaxReturn($json);
		}
		
		if($id){
			$map['id'] = $id;
			$json  = sendRest("BDC/Dict/find",$map);
			$row   = $json['data']; 
			
			$title = dejsonByLang($row['value'],$lang);
			$this->assign("title",$title);
			
			$this->assign("row",$row);
		}
		
		//获取语言列表
		$this->assign("lang",C("LANG_ARRAY"));
		
		$this->display();
	}
	/**
	 * 数据字典修改
	 */
	
	public function edit(){
	
		$lang = getLang();
		$id = $this->_param("id","intval",0);
		
		if(IS_AJAX){
			$json = sendRest("BDC/Dict/edit", $_POST);
			$this->ajaxReturn($json);
		}
		if($id){
			$map['id'] = $id;
			$json  = sendRest("BDC/Dict/find",$map);
			$row   = $json['data'];
			 
			$title = dejsonByLang($row['value'],$lang);
			$this->assign("title",$title);
			
			$value = json_decode($row['value'],true);
			$this->assign("value",$value);
			
			$this->assign("row",$row);
		}
		//获取语言列表
		$this->assign("lang",C("LANG_ARRAY"));
		
		$this->display();
	}
	/**
	 * 数据字典删除
	 */
	public function del(){
		
		if(IS_AJAX){
			$map['id'] = $this->_param("id","intval",0);
			$json = sendRest("BDC/Dict/del",$map);
			$this->ajaxReturn($json);
		}
	}
	/**
	 * 栏目排序
	 */
	function sort(){
		sendRest("BDC/Dict/sort",$_POST);
		$key = $_POST['key'];
		$this->assign("jumpUrl",U('/Dict/index','key='.$key));
		$this->success('排序成功');
	}
	
}

?>